// 根据身份证获取出生日期、性别、年龄
// 1取出生年月日(1989-03-18)，2取性别(1:男，2:女)，3取年龄
export function getInfoByIdCard(UUserCard, num) {
  if (!UUserCard) {
    return ''
  }
  const len = (UUserCard + '').length;
  if (num === 1) {
    let birth
    // 获取出生日期
    if (len === 18) {
      birth = UUserCard.substring(6, 10) + '-' + UUserCard.substring(10, 12) + '-' + UUserCard.substring(12, 14);
    }
    if (len === 15) {
      birth ='19' + UUserCard.substring(6, 8) + '-' + UUserCard.substring(8, 10) + '-' + UUserCard.substring(10, 12);
    }
    return birth;
  }
  if (num === 2) {
    // 获取性别
    let sex;
    if (len === 18) {
      if (parseInt(UUserCard.substr(16, 1)) % 2 === 1) {
        // 男
        sex = 1;
      } else {
        // 女
        sex = 0;
      }
    }
    if (len === 15) {
      if (parseInt(UUserCard.substr(14, 1)) % 2 === 1) {
        // 男
        sex = 1;
      } else {
        // 女
        sex = 0;
      }
    }
    return sex;
  }
  if (num === 3) {
    // 获取年龄
    // let myDate = new Date();
    // let month = myDate.getMonth() + 1;
    // let day = myDate.getDate();
    // let age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
    // if (
    //   UUserCard.substring(10, 12) < month ||
    //   (UUserCard.substring(10, 12) === month && UUserCard.substring(12, 14) <= day)
    // ) {
    //   age++;
    // }
    let strBirthday = '';
    if (len == 18) {
      //处理18位的身份证号码从号码中得到生日和性别代码
      strBirthday = UUserCard.substr(6, 4) + '/' + UUserCard.substr(10, 2) + '/' + UUserCard.substr(12, 2);
    }
    if (len == 15) {
      let birthdayValue = '';
      birthdayValue = UUserCard.charAt(6) + UUserCard.charAt(7);
      if (parseInt(birthdayValue) < 10) {
        strBirthday = '20' + UUserCard.substr(6, 2) + '/' + UUserCard.substr(8, 2) + '/' + UUserCard.substr(10, 2);
      } else {
        strBirthday = '19' + UUserCard.substr(6, 2) + '/' + UUserCard.substr(8, 2) + '/' + UUserCard.substr(10, 2);
      }
    }
    let birthDate = new Date(strBirthday);
    let nowDateTime = new Date();
    let age = nowDateTime.getFullYear() - birthDate.getFullYear();
    //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
    if (
      nowDateTime.getMonth() < birthDate.getMonth() ||
      (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())
    ) {
      age--;
    }
    return age;
  }
}
